package com.chengh.nami.common.utils.captchaImage;

import com.chengh.nami.common.exception.BizException;
import com.chengh.nami.common.utils.captchaImage.model.CaptchaCode;
import com.chengh.nami.common.utils.captchaImage.model.CaptchaCodeEnum;
import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import org.springframework.stereotype.Component;

/**
 * 验证码工具
 *
 * @author chengh
 */
@Component
public class CaptchaImageUtils {

    /**
     * 获取验证码生产类
     *
     * @return /
     */
    public Captcha getCaptcha() {
        CaptchaCode captchaCode = new CaptchaCode();
        captchaCode.setCodeType(CaptchaCodeEnum.ARITHMETIC);
        return switchCaptcha(captchaCode);
    }

    /**
     * 依据配置信息生产验证码
     *
     * @param captchaCode 验证码配置信息
     * @return /
     */
    private Captcha switchCaptcha(CaptchaCode captchaCode) {
        Captcha captcha;
        switch (captchaCode.getCodeType()) {
            case ARITHMETIC:
                // 算术类型 https://gitee.com/whvse/EasyCaptcha
                captcha = new FixedArithmeticCaptcha(captchaCode.getWidth(), captchaCode.getHeight());
                // 几位数运算，默认是两位
                captcha.setLen(captchaCode.getLength());
                break;
            case CHINESE:
                captcha = new ChineseCaptcha(captchaCode.getWidth(), captchaCode.getHeight());
                captcha.setLen(captchaCode.getLength());
                break;
            case CHINESE_GIF:
                captcha = new ChineseGifCaptcha(captchaCode.getWidth(), captchaCode.getHeight());
                captcha.setLen(captchaCode.getLength());
                break;
            case GIF:
                captcha = new GifCaptcha(captchaCode.getWidth(), captchaCode.getHeight());
                captcha.setLen(captchaCode.getLength());
                break;
            case SPEC:
                captcha = new SpecCaptcha(captchaCode.getWidth(), captchaCode.getHeight());
                captcha.setLen(captchaCode.getLength());
                break;
            default:
                throw new BizException("验证码配置信息错误！正确配置查看 CaptchaCode ");
        }
        return captcha;
    }

    static class FixedArithmeticCaptcha extends ArithmeticCaptcha {
        public FixedArithmeticCaptcha(int width, int height) {
            super(width, height);
        }

        @Override
        protected char[] alphas() {
            // 生成随机数字和运算符
            int n1 = num(1, 10), n2 = num(1, 10);
            // 计算结果
            int opt;
            int res;
            char optChar;
            if (n1 >= n2) {
                opt = num(3);
                res = new int[]{n1 + n2, n1 - n2, n1 * n2}[opt];
                optChar = "+-x".charAt(opt);
            } else {
                opt = num(2);
                res = new int[]{n1 + n2, n1 * n2}[opt];
                optChar = "+x".charAt(opt);
            }

            this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2));
            this.chars = String.valueOf(res);

            return chars.toCharArray();
        }
    }
}
